//
//  PageTitleView.swift
//  DouYuZB
//
//  Created by 刘耀 on 2017/4/11.
//  Copyright © 2017年 深圳多诺信息科技有限公司. All rights reserved.
//

import UIKit


// MARK: - 定义协议
protocol PageTitleViewDelegate: class {
    // class 表示只能被类遵守，不能被结构体、枚举遵守
    func pageTitleView(titleVIew: PageTitleView, index: Int)
}

// MARK: - 定义常量
private let kScrollLineH: CGFloat = 2
private let kNormalColor: (CGFloat, CGFloat, CGFloat) = (85, 85, 85)
private let kSelectColor: (CGFloat, CGFloat, CGFloat) = (255, 128, 0)


/// 头部视图
class PageTitleView: UIView {
    
    weak var delegate:PageTitleViewDelegate?
    // MARK: - 定义属性
    fileprivate var currentInex: Int = 0
    fileprivate var titles: [String]
    fileprivate var titleLabels: [UILabel] = [UILabel]()
    
    fileprivate lazy var scrollView: UIScrollView = {
       let sc = UIScrollView()
        sc.showsHorizontalScrollIndicator = false
        sc.scrollsToTop = false  // 点击返回顶部
        sc.bounces = false
        return sc
        
    }()
    fileprivate lazy var scrlllLine: UIView = {
       let line = UIView()
        line.backgroundColor = UIColor.orange
        return line
    }()

    // MARK: - 自定义构造函数
    init(frame: CGRect, titles: [String]) {
        self.titles = titles
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


// MARK: - 设置UI界面
extension PageTitleView {
    fileprivate func setupUI() {
        
        addSubview(scrollView)
        scrollView.frame = bounds
        
        setupTitleLabel()  // 添加label
        setupBottomScrollLine() //添加底线
    }
    private func setupTitleLabel() {
        
        let labelW: CGFloat = width / CGFloat(titles.count)
        let labelH: CGFloat = height - kScrollLineH
        let labelY: CGFloat = 0
        
        for (index, title) in titles.enumerated() {
            let labelX: CGFloat = labelW * CGFloat(index)
            let frame = CGRect(labelX, labelY, labelW, labelH)
            
            let label = UILabel(title: title, size: frame, fontSize: 17, alignment: .center)
            label.tag = index
            label.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
            titleLabels.append(label)
            scrollView.addSubview(label)
            label.isUserInteractionEnabled = true
            // 添加手势
            let tapGes = UITapGestureRecognizer(target: self, action: #selector(PageTitleView.titleLabelClick(tapGes:)))
            label.addGestureRecognizer(tapGes)
        }
    }
    
    private func setupBottomScrollLine() {
        let line = UIView()
        line.backgroundColor = UIColor.lightGray
        let lineH: CGFloat = 0.5
        line.frame = CGRect(0, height - lineH, width, lineH)
        addSubview(line)
        
        guard let firstLabel = titleLabels.first else { return }
        firstLabel.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        scrollView.addSubview(scrlllLine)
        scrlllLine.frame = CGRect(x: firstLabel.x, y: height - kScrollLineH, width: firstLabel.width, height: kScrollLineH)
    }

    
}

// MARK: - 监听Label的点击
extension PageTitleView {
    @objc fileprivate func titleLabelClick(tapGes: UITapGestureRecognizer) {
        // 1、获取当前label
        guard let currentL = tapGes.view as? UILabel  else { return }
        let oldLabel = titleLabels[currentInex]
        // 2、改变颜色
        currentL.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        oldLabel.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
        // 3、保存最新Label下标
        currentInex = currentL.tag
        
        // 4、滚动条的位置
        let scrollX = CGFloat(currentL.tag) * scrlllLine.width
        UIView.animate(withDuration: 0.3) {
            self.scrlllLine.x = scrollX
        }
        // 5.通知代理
        delegate?.pageTitleView(titleVIew: self, index: currentInex)
    }
}

// MARK: - 对外暴露的方法
extension PageTitleView {
    func setTitleViewProgress(progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        let sourceLabel = titleLabels[sourceIndex]
        let targetLabel = titleLabels[targetIndex]
        
        let moveTotalX = targetLabel.x - sourceLabel.x
        let moveX = moveTotalX * progress
        scrlllLine.x = sourceLabel.x + moveX
        
        //颜色发生渐变
        let colorDelta = (kSelectColor.0 - kNormalColor.0, kSelectColor.1 - kNormalColor.1, kSelectColor.2 - kNormalColor.2)
        sourceLabel.textColor = UIColor(r: kSelectColor.0 - colorDelta.0 * progress, g: kSelectColor.1 - colorDelta.1 * progress, b: kSelectColor.2 - colorDelta.2 * progress)
        targetLabel.textColor = UIColor(r: kNormalColor.0 + colorDelta.0 * progress, g: kNormalColor.1 + colorDelta.1 * progress, b: kNormalColor.2 + colorDelta.2 * progress)
        
        currentInex = targetIndex  // 记录最新的index
    }
}
